<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <el-form-item label="下单时间" prop="orderTime">
          <el-input v-model="dataForm.orderTime" placeholder="下单时间"></el-input>
      </el-form-item>
          <el-form-item label="订单状态(3.已失效 ，4，已结算， 5.已下单， 6.已支付" prop="status">
          <el-input v-model="dataForm.status" placeholder="订单状态(3.已失效 ，4，已结算， 5.已下单， 6.已支付"></el-input>
      </el-form-item>
          <el-form-item label="订单子状态" prop="orderSubStatus">
          <el-input v-model="dataForm.orderSubStatus" placeholder="订单子状态"></el-input>
      </el-form-item>
          <el-form-item label="客户描述" prop="newCustomerDesc">
          <el-input v-model="dataForm.newCustomerDesc" placeholder="客户描述"></el-input>
      </el-form-item>
          <el-form-item label="结算状态" prop="settled">
          <el-input v-model="dataForm.settled" placeholder="结算状态"></el-input>
      </el-form-item>
          <el-form-item label="渠道标识" prop="channelTag">
          <el-input v-model="dataForm.channelTag" placeholder="渠道标识"></el-input>
      </el-form-item>
          <el-form-item label="渠道名称" prop="channelName">
          <el-input v-model="dataForm.channelName" placeholder="渠道名称"></el-input>
      </el-form-item>
          <el-form-item label="维权退款标识" prop="refundFlag">
          <el-input v-model="dataForm.refundFlag" placeholder="维权退款标识"></el-input>
      </el-form-item>
          <el-form-item label="" prop="statParam">
          <el-input v-model="dataForm.statParam" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="广告推荐码" prop="adCode">
          <el-input v-model="dataForm.adCode" placeholder="广告推荐码"></el-input>
      </el-form-item>
          <el-form-item label="" prop="openId">
          <el-input v-model="dataForm.openId" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="" prop="incrementRatio">
          <el-input v-model="dataForm.incrementRatio" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="" prop="baseCommSettleRate">
          <el-input v-model="dataForm.baseCommSettleRate" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="预告佣金" prop="baseComm">
          <el-input v-model="dataForm.baseComm" placeholder="预告佣金"></el-input>
      </el-form-item>
          <el-form-item label="订单质量" prop="orderQuality">
          <el-input v-model="dataForm.orderQuality" placeholder="订单质量"></el-input>
      </el-form-item>
          <el-form-item label="" prop="baseCommSettleRateShow">
          <el-input v-model="dataForm.baseCommSettleRateShow" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="" prop="deductionAmount">
          <el-input v-model="dataForm.deductionAmount" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="佣金" prop="estiCommAmountAfterDeduction">
          <el-input v-model="dataForm.estiCommAmountAfterDeduction" placeholder="佣金"></el-input>
      </el-form-item>
          <el-form-item label="创建时间" prop="createTime">
          <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
      </el-form-item>
          <el-form-item label="更新时间" prop="updateTime">
          <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
      </el-form-item>
          <el-form-item label="推广者身份（1.二方，2，三方）" prop="promoterId">
          <el-input v-model="dataForm.promoterId" placeholder="推广者身份（1.二方，2，三方）"></el-input>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        orderTime: '',
        status: '',
        orderSubStatus: '',
        newCustomerDesc: '',
        settled: '',
        channelTag: '',
        channelName: '',
        refundFlag: '',
        statParam: '',
        adCode: '',
        openId: '',
        incrementRatio: '',
        baseCommSettleRate: '',
        baseComm: '',
        orderQuality: '',
        baseCommSettleRateShow: '',
        deductionAmount: '',
        estiCommAmountAfterDeduction: '',
        createTime: '',
        updateTime: '',
        promoterId: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        orderTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        status: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        orderSubStatus: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        newCustomerDesc: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        settled: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        channelTag: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        channelName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        refundFlag: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        statParam: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        adCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        openId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        incrementRatio: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        baseCommSettleRate: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        baseComm: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        orderQuality: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        baseCommSettleRateShow: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        deductionAmount: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        estiCommAmountAfterDeduction: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updateTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        promoterId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/adx/wphorder/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/adx/wphorder/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
